﻿using JaxaraRnD.Helpers.DataAccess;
using JaxaraRnD.Models.Objects;
using NHibernate.Event.Default;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JaxaraRnD.Controllers
{
    public class StaffController : Controller
    {
        private readonly IRepository<Staff> _ripository;

        public StaffController()
        {
            _ripository = new Repository<Staff>();
        }

        //
        // GET: /Staff/

        public ActionResult Index()
        {
            return View(_ripository.GetAll());
        }

        //
        // GET: /Staff/Details/5

        public ActionResult Details(int id)
        {
            Staff staff = _ripository.Get(id);
            staff.Orders = _ripository.GetOrdersByStuffId(staff.Id);
            
            if (staff == null)
            {
                return HttpNotFound();
            }
            return View(staff);
        }

        //
        // GET: /Staff/Create

        public ActionResult Create()
        {
            return View(new Staff());
        }

        //
        // POST: /Staff/Create

        [HttpPost]
        public ActionResult Create(Staff aObj)
        {
            if (ModelState.IsValid)
            {
                _ripository.SaveOrUpdate(aObj);
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Staff/Edit/5

        public ActionResult Edit(int id)
        {
            Staff staff = _ripository.Get(id);
            if (staff == null)
            {
                return HttpNotFound();
            }
            return View(staff);
        }

        //
        // POST: /Staff/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Staff staff)
        {
            if (ModelState.IsValid)
            {
                _ripository.SaveOrUpdate(staff);

                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }

        //
        // GET: /Staff/Delete/5

        public ActionResult Delete(int id)
        {
            _ripository.Delete(id);
            return RedirectToAction("Index");
        }      
    }
}
